import { HttpBaseImpl } from "./HttpBaseImpl";
import { loading } from '@/loading/Loading'

export class HttpUniImpl extends HttpBaseImpl {

	post(option) {
		super.post(option)
		loading.show()
		uni.request({
			url: option.url, //完整接口地址,例:https://www.example.com/request
			data: option.data || {}, //类型可为 Object/String/ArrayBuffer	
			header: option.header || {}, //App、H5端会自动带上cookie，且H5端不可手动修改
			method: 'POST',
			timeout: option.timeout || this.timeout || 60000, //单位毫秒
			dataType: option.dataType || 'json', //如果设为 json，会尝试对返回的数据做一次 JSON.parse
			success: (res) => {
				if (option.success) {
					option.success(res)
				}
			},
			fail: (res) => {
				if (option.error) {
					option.error(res)
				}
			},
			complete: (res) => {
				loading.hidden()
				if (option.complete) {
					option.complete(res)
				}
			},
		});
	}
	put(option) {
		super.put(option)
		loading.show()
		uni.request({
			url: option.url, //完整接口地址,例:https://www.example.com/request
			data: option.data || {}, //类型可为 Object/String/ArrayBuffer	
			header: option.header || {}, //App、H5端会自动带上cookie，且H5端不可手动修改
			method: 'PUT',
			timeout: option.timeout || this.timeout || 60000, //单位毫秒
			dataType: option.dataType || 'json', //如果设为 json，会尝试对返回的数据做一次 JSON.parse
			success: (res) => {
				if (option.success) {
					option.success(res)
				}
			},
			fail: (res) => {
				if (option.error) {
					option.error(res)
				}
			},
			complete: (res) => {
				loading.hidden()
				if (option.complete) {
					option.complete(res)
				}
			},
		});
	}
	delete(option) {
		super.delete(option)
		loading.show()
		uni.request({
			url: option.url, //完整接口地址,例:https://www.example.com/request
			data: option.data || {}, //类型可为 Object/String/ArrayBuffer	
			header: option.header || {}, //App、H5端会自动带上cookie，且H5端不可手动修改
			method: 'DELETE',
			timeout: option.timeout || this.timeout || 60000, //单位毫秒
			dataType: option.dataType || 'json', //如果设为 json，会尝试对返回的数据做一次 JSON.parse
			success: (res) => {
				if (option.success) {
					option.success(res)
				}
			},
			fail: (res) => {
				if (option.error) {
					option.error(res)
				}
			},
			complete: (res) => {
				loading.hidden()
				if (option.complete) {
					option.complete(res)
				}
			},
		});
	}
	get(option) {
		super.get(option)
		loading.show()
		uni.request({
			url: option.url, //完整接口地址,例:https://www.example.com/request
			data: option.data || {}, //类型可为 Object/String/ArrayBuffer	
			header: option.header || {}, //App、H5端会自动带上cookie，且H5端不可手动修改
			method: 'GET',
			timeout: option.timeout || this.timeout || 60000, //单位毫秒
			dataType: option.dataType || 'json', //如果设为 json，会尝试对返回的数据做一次 JSON.parse
			success: (res) => {
				if (option.success) {
					option.success(res)
				}
			},
			fail: (res) => {
				if (option.error) {
					option.error(res)
				}
			},
			complete: (res) => {
				loading.hidden()
				if (option.complete) {
					option.complete(res)
				}
			},
		});
	}
}